<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;
//--考试名称
class ExamName extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		if(request()->isPost()){
			//$where["学期ID"] = currTerm()["termId"];
			$where = [];
			$page = input('page');
			$rows = input('rows');
			$result = Db::table("考试名称")->alias("a")
					->join("学期名称 b","a.学期ID=b.ID","LEFT")
					->join("年级名称 c","a.年级ID=c.ID","LEFT")
					->join("年级名称 d","a.登分年级ID=d.ID","LEFT")
					->field("a.ID,年级ID,b.学期名称,c.年级名称,考试名称,登分终止日期,查看分数,登分年级ID,d.年级名称 登分年级,考试科目,满分数,考试类型")
					->where($where)
					->order("ID desc")
					->page($page, $rows)
					->select();
			$count = Db::table("考试名称")
					->where($where)
					->count();
			$grid['rows'] = selectResult($result);
			$grid['total'] = $count;
			return $grid;
		}
	}
	
	public function addUpdate() {
		$all = request()->param();
		try {
			if(array_key_exists("ID", $all)) {
				update('考试名称', $all);
			} else {
				$all["学期ID"] = currTerm()["termId"];
				insert('考试名称', $all);
				$getID = Db::query("select @@identity")[0][""];
				if($getID > 0) {
					$where["年级ID"] = array("eq",$all["年级ID"]);
					$data = Db::query("select top 4 年级ID,班级层次,培优分段,公式,".$getID."考试ID from 培优总分公式 where 年级ID = ".$all["年级ID"]." order by 考试ID desc");
					$data = selectResult($data);
					Db::table('培优总分公式')->insertAll($data);
				}
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::name('考试名称')->where('ID','in',explode(",", $delIds))->delete();
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function getCurrTermExam(){
		$where["学期ID"] = input("termId");
		$where["年级ID"] = input("grade");
		return Db::table("考试名称")
		       ->field("ID value, 考试名称 text")
		       ->where($where)
		       ->order("value desc")
		       ->select();
	}

}
